
package mapper.api;

import mapper.config.MapperApiConfigure;
import mapper.loader.PluginLoader;
import mapper.model.MappingInfo;
import mapper.utils.RequestUtil;
import mapper.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.stereotype.Component;

@Component
public class ApplicationRunner implements org.springframework.boot.ApplicationRunner {

	@Autowired
	private RequestMappingService apiMappingService;

	@Value("${mapper-api.context:}")
	private String context;

	@Autowired
	private MapperApiConfigure mapperApiConfigure;

	@Autowired
	private PluginLoader pluginLoader;

	@Override
	public void run(ApplicationArguments args) throws NoSuchMethodException {
		for(MappingInfo mappingInfo : mapperApiConfigure.getMappers()) {
			String url = mappingInfo.getUrl();
			if(StringUtil.isNotEmpty(context)) {
				url = RequestUtil.joinUrl(context, url);
			}
			apiMappingService.registerMapping(url, mappingInfo.getMethods());
		}
		pluginLoader.load();
	}
}
